package com.bysj.agent.service.impl;

import com.bysj.agent.assistant.Assistant;
import com.bysj.agent.assistant.WebSearchAssistant;
import com.bysj.agent.service.WebSearchService;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.web.search.WebSearchTool;
import dev.langchain4j.web.search.searchapi.SearchApiWebSearchEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WebSearchServiceImpl implements WebSearchService {



    @Autowired
    private SearchApiWebSearchEngine searchEngine;

    @Override
    public String search(String content) {
        ChatLanguageModel chatModel = OpenAiChatModel.builder()
                .apiKey("sk-a691c2b981cc401e99536eeca9ba7af4")
                .modelName("qwen-max")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)
                .build();
        WebSearchTool webTool = WebSearchTool.from(searchEngine);



        WebSearchAssistant assistant = AiServices.builder(WebSearchAssistant.class)
                .chatLanguageModel(chatModel)
                .tools(webTool)
                .build();

        String answer = assistant.answer(content);
        return answer;
    }
}
